package com.andy.nature.utils

/**
 * @author Andy
 * @date 2024/3/27
 * @description AnimUtils
 */
import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator

object AnimUtils {
    fun startBgAnim(startView: View, targetView: View) {
        // 获取 vTaskBg 的位置和尺寸信息
        val startLocation = IntArray(2)
        startView.getLocationInWindow(startLocation)
        val startX = startLocation[0]
        val startY = startLocation[1]
        val startWidth = startView.width
        val startHeight = startView.height
        // 获取 ivTask 的位置和尺寸信息
        val endLocation = IntArray(2)
        targetView.getLocationInWindow(endLocation)
        val endX = endLocation[0]
        val endY = endLocation[1]
        val endWidth = targetView.width
        val endHeight = targetView.height
        // 计算圆形 ivTask 的圆心和半径
        val endCx = endX + endWidth / 2f
        val endCy = endY + endHeight / 2f

        // 计算 View1 缩放后的宽高
        val scaleX = endWidth.toFloat() / startWidth
        val scaleY = endHeight.toFloat() / startHeight

        // 创建缩放动画
        val scaleXAnimator = ObjectAnimator.ofFloat(startView, View.SCALE_X, scaleX).apply {
            duration = 2500
            interpolator = DecelerateInterpolator(1.2f)
        }
        val scaleYAnimator = ObjectAnimator.ofFloat(startView, View.SCALE_Y, scaleY).apply {
            duration = 2500
            interpolator = DecelerateInterpolator(1.2f)
        }

        // 创建平移动画
        val translateX = endCx - startX - startWidth / 2
        val translateXAnimator = ObjectAnimator.ofFloat(startView, View.TRANSLATION_X, translateX).apply {
            duration = 2500
            interpolator = DecelerateInterpolator(1.2f)
        }
        val translateY = endCy - startY - startHeight / 2
        val translateYAnimator = ObjectAnimator.ofFloat(startView, View.TRANSLATION_Y, translateY).apply {
            duration = 2500
            interpolator = DecelerateInterpolator(1.2f)
        }
        // 创建动画集合
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(scaleXAnimator, scaleYAnimator, translateXAnimator, translateYAnimator)
        // 设置动画监听器
        animatorSet.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {

            }

            override fun onAnimationEnd(animation: Animator) {
                startView.apply {
                    this.scaleX = 1f
                    this.scaleY = 1f
                    this.translationX = 0f
                    this.translationY = 0f
                    this.alpha = 1f
                }
            }

            override fun onAnimationCancel(animation: Animator) {
            }

            override fun onAnimationRepeat(animation: Animator) {
            }
        })
        // 启动动画
        animatorSet.start()
    }

    fun startRotate(view: View?) {
        view?.let {
            it.clearAnimation()
            // 创建旋转动画
            val rotateAnimator1 = ObjectAnimator.ofFloat(it, View.ROTATION, 0f, 60f).apply {
                interpolator = AccelerateInterpolator(1.4f)
                duration = 400
            }

            val rotateAnimator2 = ObjectAnimator.ofFloat(it, View.ROTATION, 60f, 660f).apply {
                interpolator = LinearInterpolator()
                duration = 700
            }

            val rotateAnimator3 = ObjectAnimator.ofFloat(it, View.ROTATION, 660f, 720f).apply {
                interpolator = DecelerateInterpolator(1.4f)
                duration = 400
            }
            // 创建动画集合
            val animatorSet = AnimatorSet().apply {
                playSequentially(rotateAnimator1, rotateAnimator2, rotateAnimator3)
                addListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(animation: Animator) {
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        view.postDelayed({ startRotate(view) }, 3000)
                    }

                    override fun onAnimationCancel(animation: Animator) {
                    }

                    override fun onAnimationRepeat(animation: Animator) {
                    }
                })
            }
            animatorSet.start()
        }
    }

    fun startAnim(v: View) {
        val alpha = PropertyValuesHolder.ofFloat("alpha", 0.1f, 1f)
        val animator = ObjectAnimator.ofPropertyValuesHolder(v, alpha)
        animator.duration = 500
        animator.repeatCount = ObjectAnimator.INFINITE
        animator.repeatMode = ObjectAnimator.REVERSE
        animator.start()
    }
}
